package com.jeethink.erp.warehouse.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

import com.jeethink.common.core.domain.TreeSelect;
import com.jeethink.common.core.domain.entity.TbShop;
import com.jeethink.common.core.domain.entity.TbWarehouse;
import com.jeethink.common.utils.DateUtils;
import com.jeethink.common.utils.StringUtils;
import com.jeethink.erp.warehouse.service.ITbWarehouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.jeethink.erp.warehouse.mapper.TbWarehouseMapper;

/**
 * 仓库Service业务层处理
 * 
 * @author ypr
 * @date 2021-05-26
 */
@Service
public class TbWarehouseServiceImpl implements ITbWarehouseService
{
    @Autowired
    private TbWarehouseMapper tbWarehouseMapper;

    /**
     * 查询仓库
     * 
     * @param warehouseId 仓库ID
     * @return 仓库
     */
    @Override
    public TbWarehouse selectTbWarehouseById(Long warehouseId)
    {
        return tbWarehouseMapper.selectTbWarehouseById(warehouseId);
    }

    /**
     * 查询仓库列表
     * 
     * @param tbWarehouse 仓库
     * @return 仓库
     */
    @Override
    public List<TbWarehouse> selectTbWarehouseList(TbWarehouse tbWarehouse)
    {
        return tbWarehouseMapper.selectTbWarehouseList(tbWarehouse);
    }

    /**
     * 新增仓库
     * 
     * @param tbWarehouse 仓库
     * @return 结果
     */
    @Override
    public int insertTbWarehouse(TbWarehouse tbWarehouse)
    {
        tbWarehouse.setCreateTime(DateUtils.getNowDate());
        return tbWarehouseMapper.insertTbWarehouse(tbWarehouse);
    }

    /**
     * 修改仓库
     * 
     * @param tbWarehouse 仓库
     * @return 结果
     */
    @Override
    public int updateTbWarehouse(TbWarehouse tbWarehouse)
    {
        tbWarehouse.setUpdateTime(DateUtils.getNowDate());
        return tbWarehouseMapper.updateTbWarehouse(tbWarehouse);
    }

    /**
     * 批量删除仓库
     * 
     * @param warehouseIds 需要删除的仓库ID
     * @return 结果
     */
    @Override
    public int deleteTbWarehouseByIds(Long[] warehouseIds)
    {
        return tbWarehouseMapper.deleteTbWarehouseByIds(warehouseIds);
    }

    /**
     * 删除仓库信息
     * 
     * @param warehouseId 仓库ID
     * @return 结果
     */
    @Override
    public int deleteTbWarehouseById(Long warehouseId)
    {
        return tbWarehouseMapper.deleteTbWarehouseById(warehouseId);
    }

    @Override
    public List<TreeSelect> buildWareHouseTreeSelect(List<TbWarehouse> tbWarehouse) {
        List<TbWarehouse> areHouseTrees = buildWarehouseTree(tbWarehouse);
        return areHouseTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
    }

    @Override
    public List<TbWarehouse> buildWarehouseTree(List<TbWarehouse> tbWarehouses) {
        List<TbWarehouse> returnList = new ArrayList<TbWarehouse>();
        List<Long> tempList = new ArrayList<Long>();
        for (TbWarehouse warehouse : tbWarehouses)
        {
            tempList.add(warehouse.getWarehouseId());
        }
        for (Iterator<TbWarehouse> iterator = tbWarehouses.iterator(); iterator.hasNext();)
        {
            TbWarehouse warehouse = (TbWarehouse) iterator.next();
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(warehouse.getParentId()))
            {
                recursionFn(tbWarehouses, warehouse);
                returnList.add(warehouse);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = tbWarehouses;
        }
        return returnList;
    }

    /**
     * 递归列表
     */
    private void recursionFn(List<TbWarehouse> list, TbWarehouse t)
    {
        // 得到子节点列表
        List<TbWarehouse> childList = getChildList(list, t);
        t.setChildren(childList);
        for (TbWarehouse tChild : childList)
        {
            if (hasChild(list, tChild))
            {
                recursionFn(list, tChild);
            }
        }
    }


    /**
     * 得到子节点列表
     */
    private List<TbWarehouse> getChildList(List<TbWarehouse> list, TbWarehouse t)
    {
        List<TbWarehouse> tlist = new ArrayList<TbWarehouse>();
        Iterator<TbWarehouse> it = list.iterator();
        while (it.hasNext())
        {
            TbWarehouse n = (TbWarehouse) it.next();
            if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == t.getWarehouseId().longValue())
            {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<TbWarehouse> list, TbWarehouse t)
    {
        return getChildList(list, t).size() > 0 ? true : false;
    }

}
